package com.xnyzc.lhy.order.service.impl.work;

import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.util.CalculationUtil;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.order.entity.work.OaDWorkStatistics;
import com.xnyzc.lhy.order.service.work.IOaDWorkStatisticsService;
import com.xnyzc.lhy.order.service.work.IOaDWorkTimeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 工作时间
 * @Author zhuyubin
 * @Date 2019/8/3 13:59
 **/
@Slf4j
@Service
public class OaDWorkTimeServiceImpl implements IOaDWorkTimeService {

    @Autowired
    private IOaDWorkStatisticsService iOaDWorkStatisticsService;

    /**
     * 获取在线时长
     * @param driverId
     * @param tag
     * @return
     */
    @Override
    public Map<String, Object> getWorkTime(Long driverId, String tag){
        OaDWorkStatistics oaDWorkStatistics = iOaDWorkStatisticsService.getWork(driverId, tag);
        // 初始化响应字段
        Map<String, Object> map = this.initWorkTimeMap();
        if (CheckUtil.objIsNotEmpty(oaDWorkStatistics)) {
            String dayOnlineTimeH;
            String dayOrderTimeH;
            String dayServerTimeH;
            String dayRestTimeH;
            try {
                Integer dayOnlineTime = oaDWorkStatistics.getDayOnlineTime();
                Integer dayOrderTime = oaDWorkStatistics.getDayOrderTime();
                Integer dayServerTime = oaDWorkStatistics.getDayServerTime();
                Integer dayRestTime = oaDWorkStatistics.getDayRestTime();
                dayOnlineTimeH = CalculationUtil.divide(String.valueOf(dayOnlineTime), "60", 2);
                dayOrderTimeH = CalculationUtil.divide(String.valueOf(dayOrderTime), "60", 2);
                dayServerTimeH = CalculationUtil.divide(String.valueOf(dayServerTime), "60", 2);
                dayRestTimeH = CalculationUtil.divide(String.valueOf(dayRestTime), "60", 2);
            } catch (IllegalAccessException e) {
                throw PangException.create("计算除法出错");
            }
            map.put("onlineTime", dayOnlineTimeH);
            map.put("orderTime", dayOrderTimeH);
            map.put("serverTime", dayServerTimeH);
            map.put("restTime", dayRestTimeH);
        }
        return map;
    }

    /**
     * 初始化map
     * @return
     */
    private Map<String, Object> initWorkTimeMap() {
        Map<String, Object> map = new HashMap<>(16);
        map.put("onlineTime", "");
        map.put("orderTime", "");
        map.put("serverTime", "");
        map.put("restTime", "");
        map.put("onlineTimeMessage", "已经连续驾驶x时间,避免疲劳驾驶");
        map.put("workAwayMessage", "连续服务x小时 需休息x时间,否则将强制收车");
        map.put("fatigueDrivingRule", "疲劳驾驶规则(HTML)");
        return map;
    }
}
